package com.ck.mybatis.plus.controller;

import java.util.List;
import com.ck.mybatis.plus.entity.dto.UserDTO;
import com.ck.mybatis.plus.entity.dto.UserPageDTO;
import com.ck.mybatis.plus.entity.vo.UserPageVO;
import com.ck.mybatis.plus.entity.vo.UserVO;
import com.ck.mybatis.plus.model.CkPageInfo;
import com.ck.mybatis.plus.model.WrappedResult;
import com.ck.mybatis.plus.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Classname UserController
 * @Description TODO
 * @Date 2020/7/11 15:03
 * @Created by lpc
 */
@RestController
@RequestMapping("/demo")
@Api(value = "示例", tags = "示例")
public class UserController {


    @Autowired
    private UserService service;

    @ApiOperation("保存")
    @PostMapping("/insert")
    public WrappedResult<Long> insert(@Valid @RequestBody UserDTO userDTO) {
        Long id = service.insert(userDTO);
        return WrappedResult.success(id);

    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public WrappedResult<Boolean> update(@RequestBody UserDTO userDTO) {
        Boolean result = service.update(userDTO);
        return WrappedResult.success(result);
    }

    @ApiOperation("删除")
    @GetMapping("/delete")
    public WrappedResult<Boolean> delete(@RequestParam Long id) {
        Boolean result = service.delete(id);
        return WrappedResult.success(result);
    }


    @ApiOperation("详情")
    @GetMapping("/get")
    public WrappedResult<UserVO> get(@RequestParam Long id) {
        UserVO demoVO = service.get(id);
        return WrappedResult.success(demoVO);
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    public WrappedResult<CkPageInfo<UserPageVO>> page(@RequestBody UserPageDTO userPageDTO) {
        CkPageInfo<UserPageVO> page = service.page(userPageDTO);
        return WrappedResult.success(page);
    }


    /**
     * 单表多个条件查询demo
     * @param userPageDTO
     * @return
     * @author jiangqingpeng
     * @throws Exception
     */
    @ApiOperation("单表多个条件查询demo")
    @PostMapping("/pageWhere")
    public WrappedResult<CkPageInfo<UserPageVO>> pageWhere(@RequestBody(required = false) UserPageDTO userPageDTO) throws Exception {
        CkPageInfo<UserPageVO> page = service.pageWhere(userPageDTO);
        return WrappedResult.success(page);
    }


    /**
     * 多表多个条件查询demo
     * @param userPageDTO
     * @return
     * @author jiangqingpeng
     * @throws Exception
     */
    @ApiOperation("多表多个条件查询demo")
    @PostMapping("/pageJoinWhere")
    public WrappedResult<CkPageInfo<UserPageVO>> pageJoinWhere(@RequestBody(required = false) UserPageDTO userPageDTO) throws Exception {
        CkPageInfo<UserPageVO> page = service.pageJoinWhere(userPageDTO);
        return WrappedResult.success(page);
    }
}
